﻿using HelixToolkit.Wpf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Shapes;

namespace WpfApp5
{
    /// <summary>
    /// jxbWindow.xaml 的交互逻辑
    /// </summary>
    public partial class jxbWindow : Window
    {
        public jxbWindow()
        {
            InitializeComponent();
            this.Loaded += JxbWindow_Loaded;
        }

        private GeometryModel3D _lastOperGeometryModel3D = null;

        private List<ModelUIElement3D> modelUIElement3Ds = new List<ModelUIElement3D>();

        private void JxbWindow_Loaded(object sender, RoutedEventArgs e)
        {
            string[] modelFiles = Directory.GetFiles($"{Environment.CurrentDirectory}\\3D_Models");
            if (modelFiles.Length == 0) return;
            ModelImporter modelImporter = new ModelImporter();
            foreach (string modelFile in modelFiles) 
            {
                Model3DGroup model3DGroup = modelImporter.Load(modelFile);
                GeometryModel3D? geometryModel3D = model3DGroup.Children[0] as GeometryModel3D;
                if(geometryModel3D != null) 
                {
                    ModelUIElement3D mui = new ModelUIElement3D();

                    //if (modelFile.IndexOf("LINK1_CAD") > -1) 
                    //{
                    //    AxisAngleRotation3D rotation3D = new AxisAngleRotation3D();
                    //    rotation3D.Angle = 90;
                    //    rotation3D.Axis = new Vector3D(0, 0, 1);
                    //    RotateTransform3D rotateTransform3D = new RotateTransform3D(rotation3D, 0, 0, 0);
                    //    Transform3DGroup transform3DGroup = new Transform3DGroup();
                    //    transform3DGroup.Children.Add(rotateTransform3D);
                    //    mui.Transform = transform3DGroup;
                    //}
                    

                    mui.MouseLeftButtonDown += Mui_MouseLeftButtonDown;
                    geometryModel3D.Material = new DiffuseMaterial(Brushes.White);
                    geometryModel3D.BackMaterial = new DiffuseMaterial(Brushes.White);
                    mui.Model = geometryModel3D;
                    jxb_control.Children.Add(mui);
                    modelUIElement3Ds.Add(mui);
                    
                }
                
            }
            SetTransform();
        }

        private void SetTransform()
        {
            var tg1 = new Transform3DGroup();
            var tt1 = new TranslateTransform3D(0, 0, 0);
            var rt1 = new RotateTransform3D(
                    new AxisAngleRotation3D(
                        new Vector3D(0, 0, 1),
                        0),
                    new Point3D(0, 0, 0));
            tg1.Children.Add(tt1);
            tg1.Children.Add(rt1);
            modelUIElement3Ds[1].Transform = tg1;
            modelUIElement3Ds[4].Transform = tg1;


            var tg2 = new Transform3DGroup();
            var tt2 = new TranslateTransform3D(0, 0, 0);
            var rt2 = new RotateTransform3D(
                    new AxisAngleRotation3D(
                        new Vector3D(0, 1, 0),
                        0),
                    new Point3D(175, -200, 500));
            tg2.Children.Add(tt2);
            tg2.Children.Add(rt2);
            tg2.Children.Add(tg1);
            modelUIElement3Ds[2].Transform = tg2;
            modelUIElement3Ds[5].Transform = tg2;

            var tg3 = new Transform3DGroup();
            var tt3 = new TranslateTransform3D(0, 0, 0);
            var rt3 = new RotateTransform3D(
                    new AxisAngleRotation3D(
                        new Vector3D(0, 1, 0),
                        0),
                    new Point3D(190, -700, 1595));
            tg3.Children.Add(tt3);
            tg3.Children.Add(rt3);
            tg3.Children.Add(tg2);
            modelUIElement3Ds[3].Transform = tg3;
            modelUIElement3Ds[6].Transform = tg3;
            modelUIElement3Ds[7].Transform = tg3;


            var tg4 = new Transform3DGroup();
            var tt4 = new TranslateTransform3D(0, 0, 0);
            var rt4 = new RotateTransform3D(
                    new AxisAngleRotation3D(
                        new Vector3D(1, 0, 0),
                        0),
                    new Point3D(400, 0, 1765));
            tg4.Children.Add(tt4);
            tg4.Children.Add(rt4);
            tg4.Children.Add(tg3);
            modelUIElement3Ds[8].Transform = tg4;


            var tg5 = new Transform3DGroup();
            var tt5 = new TranslateTransform3D(0, 0, 0);
            var rt5 = new RotateTransform3D(
                    new AxisAngleRotation3D(
                        new Vector3D(0, 1, 0),
                        0),
                    new Point3D(1405, 50, 1765));
            tg5.Children.Add(tt5);
            tg5.Children.Add(rt5);
            tg5.Children.Add(tg4);
            modelUIElement3Ds[9].Transform = tg5;


            var tg6 = new Transform3DGroup();
            var tt6 = new TranslateTransform3D(0, 0, 0);
            var rt6 = new RotateTransform3D(
                    new AxisAngleRotation3D(
                        new Vector3D(1, 0, 0),
                        0),
                    new Point3D(1405, 0, 1765));
            tg6.Children.Add(tt6);
            tg6.Children.Add(rt6);
            tg6.Children.Add(tg5);
            modelUIElement3Ds[10].Transform = tg6;
        }

        private void Mui_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GeometryModel3D? geometryModel3D = (sender as ModelUIElement3D).Model as GeometryModel3D;
            if (_lastOperGeometryModel3D != null) 
            {
                _lastOperGeometryModel3D.Material = new DiffuseMaterial(Brushes.White);
                _lastOperGeometryModel3D.BackMaterial = new DiffuseMaterial(Brushes.White);
            }
            if (geometryModel3D != null) 
            {
                geometryModel3D.BackMaterial = new DiffuseMaterial(Brushes.Purple);
                geometryModel3D.Material = new DiffuseMaterial(Brushes.Purple);
                _lastOperGeometryModel3D = geometryModel3D;
            }
        }

        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            int index = int.Parse((sender as Slider).Tag.ToString());
            (((modelUIElement3Ds[index].Transform as Transform3DGroup).Children[1] as RotateTransform3D)
                .Rotation as AxisAngleRotation3D).Angle = e.NewValue;
        }
    }
}
